<template>
  <div class="input-line">
    <div class="title">进度甘特图</div>
    <el-button
      size="mini"
      type="primary"
      class="upload-btn icon-btn"
      @click="handleExport"
      v-auth="`/process/main/detail--export`"
      >导出</el-button
    >
    <div>
      <el-upload
        :action="uploadUrl"
        :headers="uploadHeaders"
        accept=".xml"
        name="uploadFile"
        :on-error="handleError"
        :on-success="handleSuccess"
        :show-file-list="false"
        :before-upload="beforeAvatarUpload"
      >
        <el-button
          v-if="!props.isDataLength"
          size="mini"
          type="primary"
          v-auth="`/process/main/detail--import`"
          >导入</el-button
        >
      </el-upload>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { defineEmits, defineProps } from "vue";
import { storageLocal } from "/@/utils/storage";
import NotifMessage from "/@/utils/message/notification";
import { useRouter } from "vue-router";

const props = defineProps({
  isDataLength: {
    type: Boolean,
    default: false
  }
});

let router = useRouter();
// let route = useRoute();

let uploadUrl =
  import.meta.env.VITE_BASE_URL + "/epms-admin/flowProjectProgress/import/xml";

let uploadHeaders = {
  Authorization: storageLocal.getItem("info")?.accessToken
};

const emit = defineEmits(["emitImportGantt", "emitExportGantt"]);

const handleSuccess = file => {
  console.log(file, "file");
  if (file.code === 200) {
    emit("emitImportGantt", JSON.parse(file.data));
  }
};

const beforeAvatarUpload = file => {
  const type = file.name.substring(file.name.lastIndexOf(".") + 1);
  if (!["xml"].includes(type)) {
    NotifMessage.warning(`请上传xml格式文件!`);
    return false;
  }

  const isLt2M = file.size / 1024 / 1024 < 20;

  if (!isLt2M) {
    NotifMessage.warning(`文件大小不能超过20M!`);
    return false;
  }
};

const handleError = err => {
  let myError = err.toString(); //转字符串
  console.log(myError, "error");
  myError = myError.replace("Error: ", ""); // 去掉前面的" Error: "
  myError = JSON.parse(myError); //转对象

  myError?.message && NotifMessage.error(myError?.message);
  if (myError?.body?.code && [401, 403].includes(myError?.body?.code)) {
    myError?.body?.message && NotifMessage.error(myError?.body?.message);
    router.push("/login");
  }
};

const handleExport = () => {
  emit("emitExportGantt");
};
</script>

<style lang="scss" scoped>
.input-line {
  display: flex;
  align-items: center;
  padding: 10px 0;
  .title {
    font-weight: 600;
  }
  .input-title {
    float: left;
    line-height: 40px;
    font-size: 13px;
    text-align: right;
    width: 80px;
    .text {
      color: #666;
    }
  }
  .input {
    float: left;
    width: 400px;
    margin-left: 16px;
  }

  .icon-btn {
    // margin-top: 5px;
    margin-left: 16px;
    margin-right: 16px;
    // float: left;
    // line-height: 30px;
    // padding-right: 20px;
    // padding-left: 20px;
    // background-color: #409eff;
    // border-radius: 4px;
    // color: #fff;
    // font-size: 12px;
    // cursor: pointer;
    // transition: all 0.35s;
  }

  .upload-btn {
    &:hover {
      opacity: 0.8;
    }
  }
  .linkdown {
    display: inline-block;
    margin-left: 30px;
    font-size: 14px;
    span {
      line-height: 19px;
    }
  }
}
</style>
